package com.openxt.nbzj.splashscreen;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.merhold.extensiblepageindicator.ExtensiblePageIndicator;
import com.openxt.nbzj.R;

import java.util.ArrayList;
import java.util.List;

import qs.openxt.libs.App;
import com.alibaba.fastjson.JSONArray;
import qs.openxt.libs.splashscreen.MyPagerAdapter;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.MResource;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/5/17.
 */
public class SplashScreenPage implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private Context context;
    private Dialog dialog = null;
    private ViewPager viewPager = null;
    private List<View> mList = null;
    private MyPagerAdapter vpAdapter = null;
    private int resourceId;
    private JSONArray IMGS = null;
    private ImageButton btn_skip = null;
    private ImageButton btn_close = null;
    private RelativeLayout default_splashscreen_001 = null, default_splashscreen_002 = null;
    private ExtensiblePageIndicator extensiblePageIndicator;

    public SplashScreenPage(Context context, int resourceId){
        this.context = context;
        this.resourceId = resourceId;
    }

    public void loadPage(){
        try {
            IMGS = new JSONArray();
            IMGS.add(R.mipmap.p1);
            IMGS.add(R.mipmap.p2);
            IMGS.add(R.mipmap.p3);
            String isFirstIn = Utils.getLocalStorage(context, App.SPLASHSCREEN_ISFIRST);
            if(StringUtil.isNull(isFirstIn))
                isFirstIn = "true";
            boolean isShow = "true".equals(isFirstIn) ? true : false;
            View splashscreen = LayoutInflater.from(context).inflate(R.layout.splashscreen, null);
            if(isShow)
                showSwitchPage(splashscreen);
            showWaitPage(splashscreen);
            dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
            if ((((Activity) context).getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
            dialog.setContentView(splashscreen);
            dialog.setCancelable(false);
            dialog.show();
        }catch (Exception e){

        }
    }

    private void showSwitchPage(View splashscreen){
        btn_skip = (ImageButton) splashscreen.findViewById(R.id.btn_skip);
        btn_close = (ImageButton) splashscreen.findViewById(R.id.btn_close);
        viewPager = (ViewPager) splashscreen.findViewById(R.id.viewpager);
        viewPager.setVisibility(View.VISIBLE);
        extensiblePageIndicator = (ExtensiblePageIndicator)splashscreen.findViewById(R.id.flexibleIndicator);

        btn_skip.setVisibility(View.VISIBLE);
        btn_skip.setOnClickListener(this);
        btn_close.setOnClickListener(this);

        mList = new ArrayList<>();
        for (int i = 0, j = IMGS.size(); i < j; i++) {
            int resourceId = IMGS.getInteger(i);
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setImageResource(resourceId);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if(i == j - 1){
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closePage();
                    }
                });
            }

            mList.add(imageView);
        }

        // 初始化Adapter
        vpAdapter = new MyPagerAdapter(mList, context);
        viewPager.setAdapter(vpAdapter);
        extensiblePageIndicator.initViewPager(viewPager);
        // 绑定回调
        viewPager.setOnPageChangeListener(this);
    }

    private void showWaitPage(View splashscreen){
        default_splashscreen_001 = (RelativeLayout) splashscreen.findViewById(R.id.default_splashscreen_001);
        default_splashscreen_001.setVisibility(View.VISIBLE);
        default_splashscreen_002 = (RelativeLayout) splashscreen.findViewById(R.id.default_splashscreen_002);
        default_splashscreen_002.setVisibility(View.VISIBLE);
        //是否显示待加载页面
        TextView textView = (TextView) splashscreen.findViewById(R.id.tv_version);
        String version = "版本号 : " + ActivityUtil.getVersion(context);
        textView.setText(version);

        ImageView imgView = (ImageView) splashscreen.findViewById(R.id.start_page_img);
        imgView.setBackgroundResource(resourceId);

        ImageView loadingImage = (ImageView) splashscreen.findViewById(R.id.iv_loading0);
        if (loadingImage.getBackground() instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) loadingImage.getBackground();
            animationDrawable.start();
        }
    }

    public void close(){
        if(dialog == null)
            return;

        if (default_splashscreen_001 != null) {
            default_splashscreen_001.setVisibility(View.GONE);
        }
        if (default_splashscreen_002 != null) {
            default_splashscreen_002.setVisibility(View.GONE);
        }
        if(viewPager == null || viewPager.getVisibility() == View.GONE)
            dialog.dismiss();

        Utils.setLocalStorage(context, App.SPLASHSCREEN_ISFIRST, "false");
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position != 2) {
            btn_skip.setVisibility(View.VISIBLE);
            btn_close.setVisibility(View.GONE);
        }else {
            btn_skip.setVisibility(View.GONE);
            btn_close.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_skip){
            closePage();
        }else if(v.getId() == R.id.btn_close){
            closePage();
        }
    }

    private void closePage(){
        btn_skip.setVisibility(View.GONE);
        viewPager.setVisibility(View.GONE);
        if(default_splashscreen_001 == null)
            close();

        if(default_splashscreen_001 != null && default_splashscreen_001.getVisibility() == View.GONE)
            close();
    }
}
